import { getCategoryAPI } from "@/apis/category";
import { onMounted } from "vue";
import { ref } from "vue";
import { useRoute } from "vue-router";
import { onBeforeRouteUpdate } from "vue-router";
// 封装分类数据业务相关代码

export function useCategory() {
  const categoryData = ref<any>({});
  const route = useRoute();

  const getCategory = async (id = route.params.id) => {
    const res: any = await getCategoryAPI(id);
    categoryData.value = res.result;
  };

  onMounted(() => {
    getCategory();
  });

  // 路由参数变化时，可以把分类数据接口重新发送
  onBeforeRouteUpdate((to) => {
    getCategory(to.params.id);
  });

  return {
    categoryData
  };
}
